<template>
  <div>
    <!--  订单日志  -->
    <el-dialog
      title="编辑cookie"
      :visible.sync="open"
      width="600px" @close="closeModal">
      <div class="edit-box">
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-form-item label="状态" prop="cookieStatus" v-if="form.cookieStatus">
            <el-input v-model="form.cookieStatus" placeholder="" disabled></el-input>
          </el-form-item>
          <el-form-item label="错误信息" prop="cookieErrorMsg" v-if="form.cookieErrorMsg">
            <el-input v-model="form.cookieErrorMsg" placeholder="" disabled/>
          </el-form-item>
          <el-form-item label="cookie" prop="cookie">
            <el-input v-model="form.cookie" type="textarea" :autosize="{ minRows: 2, maxRows: 10}"
                      placeholder="请输入cookie信息"/>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeModal">取 消</el-button>
        <el-button type="primary" @click="confirmContent">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {addCookie, queryCookieInd, editCookie} from "@/api/shop/index";
import {checkCookie} from "@/api/autoOperation/python";

export default {
  name: 'form-modal',
  props: {
    storeId: {
      type: String / Number
    },
    storeUserName: {}
  },
  components: {},
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // storeId: [{required: true, message: "店铺名称不能为空", trigger: "change"}],
        // storeBody: [{required: true, message: "店铺主体不能为空", trigger: "blur"}],
        // contacts: [{required: true, message: "店铺联系人不能为空", trigger: "blur"}],
        cookieErrorMsg: [{required: false, message: "错误信息不能为空", trigger: "blur"}],
        // validityStart: [{required: true, message: "请选择店铺生效时间", trigger: "change"}],
        // validityEnd: [{required: true, message: "请选择店铺失效时间", trigger: "change"}],
        cookieStatus: [{required: false, message: "cookie状态不能为空", trigger: "change"}],
        cookie: [{required: true, message: "请输入cookie信息", trigger: "blur"}]
      },
      open: true,
      groupChildOrderList: [],//订单日志列表
    }
  },
  created() {
    this.getCookieInd()
    // this.selectHandoverOrderDetails()
  },
  methods: {
    // 获取channelId
    getCookieInd() {
      queryCookieInd({storeId: this.storeId}).then(res => {
        console.log('cookie信息', res)
        if (res.data) {
          this.form = res.data
        }
      })
    },
    // 提交信息
    confirmContent() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.storeId = this.storeId
          this.form.storeUserName = this.storeUserName
          checkCookie({cookie_str: this.form.cookie}).then(res => { //校验cookie
            this.form.channelId = res.data.channelId
            if (this.storeUserName == res.data.cn) {
              if (this.form.id != null) {
                editCookie(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.closeModal();
                }).finally(() => {
                });
              } else {
                addCookie(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.closeModal();
                })
              }
            } else {
              this.$message.error('该店铺CN码与cookie不符！');
            }
          })
        }
      });
    },
    // 关闭线上发货弹窗
    closeModal() {
      this.$emit('close', false)
    },
  }
}
</script>
<style scoped>

</style>
